package main.java.lock;

public class SynchronizedDemo implements Runnable {

    // 共享的全局变量
    private static int count = 1;

    static long time1;

    @Override
    public void run() {
        while (true) {
            Integer count = getCount();
            if (count >= 1000) {
                long time2 = System.currentTimeMillis();
                int time = (int) ((time2 - time1)/1000);
                System.out.println("执行了："+time+"秒！");
                break;
            }
            System.out.println(count);
        }
    }

    // 使用synchronize
    private synchronized Integer getCount() {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return count++;
    }

    public static void main(String[] args) {
//        time1 = System.currentTimeMillis();
//        SynchronizedDemo userSynchronize = new SynchronizedDemo();
//        for (int index = 0; index < 10; index++) {
//            Thread t = new Thread(userSynchronize);
//            t.start();
//        }
        System.out.println(1 << 31);
    }
}
